package com.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.product.entity.BrandEntity;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mall.product.entity.CategoryBrandRelationEntity;
import com.mall.product.service.CategoryBrandRelationService;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.R;


/**
 * 品牌分类关联
 *
 * @author super
 * @email 3217985254@qq.com
 * @date 2023-07-16 17:13:15
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 根据品牌ID获取品牌关联分类信息
     *
     * @param id
     */
    @RequestMapping(value = "/catelog/list/{id}", method = RequestMethod.GET)
    public R connectList(@PathVariable("id") Long id) {
        List<CategoryBrandRelationEntity> connectList = categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", id));
        return R.ok().put("data", connectList);
    }

    /**
     * 保存关联关系
     */
    @RequestMapping(value = "/saveConnect",method = RequestMethod.POST)
    @ResponseBody
    public R saveConnect(@RequestParam("brandId") Long brandId, @RequestParam("catelogId") Long catelogId) {
        categoryBrandRelationService.saveConnectDetail(brandId, catelogId);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取分类关联的品牌
     * /product/categorybrandrelation/brands/list
     */
    @RequestMapping("/brands/list")
    public R getBrandsList(@RequestParam(value = "catId", required = true) Long catId) {
        List<CategoryBrandRelationEntity> brandList = categoryBrandRelationService.getBrandsList(catId);
        return R.ok().put("data",brandList);
    }

}
